import {BasicColumn} from '/@/components/Table';
import {FormSchema} from '/@/components/Table';
import { rules} from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { UploadBizType } from '/@/enums/uploadEnum';

export const columns: BasicColumn[] = [
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 80,
    customRender: (record) => {
      return render.renderAvatar(record)
    },
  },
  {
    title: '用户账号',
    dataIndex: 'userName',
    width: 100,
  },
  {
    title: '用户姓名',
    dataIndex: 'realName',
    width: 100,
  },
  {
    title: '手机号',
    dataIndex: 'phone',
    width: 100,
  },
  {
    title: '状态',
    dataIndex: 'status',
    customRender: ({text}) => {
      let color = 'green'
      if (text === '2') {
        color = 'red'
      }
      return render.renderTag(render.renderDict(text, 'user_status'), color)
    },
    width: 60,
  }
];

export const recycleColumns: BasicColumn[] = [
  {
    title: '用户账号',
    dataIndex: 'userName',
    width: 100,
  },
  {
    title: '用户姓名',
    dataIndex: 'realName',
    width: 100,
  },
  {
    title: '头像',
    dataIndex: 'avatar',
    width: 80,
    customRender: render.renderAvatar,
  },
  {
    title: '性别',
    dataIndex: 'sex',
    width: 80,
    sorter: true,
    customRender: ({text}) => {
      return  render.renderDict(text, 'sys_user_sex')
    }
  }
];

export const searchFormSchema: FormSchema[] = [
  {
    label: '账号',
    field: 'userName',
    component: 'JInput',
  },
  {
    label: '性别',
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode:'sys_user_sex',
      placeholder:'请选择性别',
      stringToNumber:true
    }
  },
  {
    label: '真实名称',
    field: 'realName',
    component: 'Input',
  },
  {
    label: '手机号码',
    field: 'phone',
    component: 'Input'
  },
  {
    label: '用户状态',
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode:'user_status',
      placeholder:'请选择状态',
      stringToNumber:true
    }
  }
];

export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false
  },
  {
    label: '用户账号',
    field: 'userName',
    component: 'Input',
    dynamicDisabled: ({values}) => {
      return !!values.id;
    },
    // dynamicRules: ({model,schema}) =>rules.duplicateCheckRule("sys_user", "userName",model,schema,true),
  },
  
  {
    label: '登录密码',
    field: 'password',
    component: 'StrengthMeter',
    componentProps: {
      placeholder: '请输入',
      autocomplete: 'false'
    },
    rules: [
      {
        required: true,
        message: '请输入登录密码',
      },
    ],
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({values}) => rules.confirmPassword(values,true)
  },
  {
    label: '用户姓名',
    field: 'realName',
    component: 'Input',
  },
  {
    label: '头像',
    field: 'avatar',
    component: 'MyImageUpload',
    componentProps: {
      fileMax:1,
      bizType: UploadBizType.AVATAR
    },
  },
  {
    label: '生日',
    field: 'birthday',
    component: 'DatePicker',
  },
  {
    label: '性别',
    field: 'sex',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode:'sys_user_sex',
      placeholder:'请选择',
      // stringToNumber: true
    },
  },
  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    rules: rules.rule('email', false),
    dynamicRules: ({model, schema}) => {
      if (!model.id || (model.id && model.email?.indexOf('*') !== -1)) {
        return [ { required: false } ]
      }
      return rules.rule('email', false);
    },
  },
  {
    label: '手机号码',
    field: 'phone',
    component: 'Input',
    dynamicRules: ({model, schema}) => {
      if (!model.phone || (model.id && (model.phone?.indexOf('*') !== -1))) {
        return [ { required: false } ]
      }
      return [
        { pattern: /^1[3|4|5|7|8|9][0-9]\d{8}$/, message: '手机号码格式有误' }
      ];
    },
  },
  {
    label: '座机',
    field: 'telephone',
    component: 'Input',
    rules: [{ pattern: /^0\d{2,3}-[1-9]\d{6,7}$/, message: '请输入正确的座机号码' }]
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
      rows: 3,
    },
  }
];

export const formPasswordSchema: FormSchema[] = [
  {
    label: '用户账号',
    field: 'userName',
    component: 'Input',
    componentProps: {readonly: true}
  },
  {
    label: '',
    field: 'id',
    show: false,
    component: 'Input'
  },
  {
    label: '登录密码',
    field: 'password',
    component: 'StrengthMeter',
    componentProps: {
      placeholder: '请输入登录密码',
    },
    rules: [
      {
        required: true,
        message: '请输入登录密码',
      },
    ],
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({values}) => rules.confirmPassword(values,true)
  }
];


/**
 * 搜索用户
 */
export const searchUserFormSchema: FormSchema[] = [
  {
    field: 'userName',
    label: '用户账号',
    component: 'Input',
    colProps: { span: 12 },
  },
  {
    field: 'test',
    label: '',
    component: 'Input',
    slot: 'addUser',
    colProps: { span: 12 },
  },

];
